package pure.ui 
{
	import pure.engine.system.SystemManager;
	import pure.ui.core.Component;
	import pure.ui.skins.ISkin;
	import pure.ui.skins.ScrollSkin;
	import flash.display.Bitmap;
	import flash.display.DisplayObject;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.geom.Rectangle;

	/**
	 * 滚动条
	 * @author des
	 * <皮肤种类> ---- ScrollSkin
	 *   
	 * |▲| ---- 后退按钮
	 * | |
	 * | |
	 * |■| ---- 滑动按钮
	 * | |
	 * | | ---- 滑动跑道
	 * | |
	 * | |
	 * |▼| ---- 前进按钮
	 * 
	 * <TODO>点击滑动跑道只调整一段 - (固定为滑动按钮长度) - ???未完成
	 * <TODO>滑轮事件由具体情况添加侦听 - (一般为垂直)
	 * 
	 * <TODO>垂直型滚动，状态值与滑动器状态值相反，故用最大值减去得到
	 * 
	 */
	
public class ScrollBar extends Component 
{
	/**
	 * @param x				坐标X
	 * @param y				坐标Y
	 * @param size			组件大小
	 * @param skin			皮肤
	 * @param isVertical	是否为垂直型
	 **/
	public function ScrollBar(size:Point, skin:ISkin = null, isVertical:Boolean = true, autoMove:Boolean = true, x:Number = 0, y:Number = 0) 
	{
		this._isVertical = isVertical;
		super(x,y,skin);
		if (size) 
		{
			setSize(size.x, size.y);
		}
		else
		{
			if (this._isVertical) 
			{
				setSize(MIN, MAX);
			}
			else 
			{
				setSize(MAX, MIN);
			}
		}
		this._autoMove = autoMove;
	}
	
	
	/*default*/
	
	protected static const MIN:Number = 20;
	
	protected static const MAX:Number = 190;
	
	/*model*/

	protected var _isVertical:Boolean;	// 是否为垂直型(初始化后将无法改变)
		
	/*cache*/
	
	protected var _target:DisplayObject;	// 滚动显示对象
	
	protected var _backward:Button;	// 后退按钮
	
	protected var _forward:Button;	// 前进按钮
	
	protected var _slider:Slider;	// 滑动器

	protected var _btnRoll:int = 9;	// 两侧按钮滚动像素

	protected var _autoMove:Boolean;
	
	protected var _autoHide:Boolean = true;	// 自动隐藏滚动条

	protected var _maskWidth:Number;	// 显示对象视域宽度
	
	protected var _maskHeight:Number;	// 显示对象视域高度
	
	protected var _trackSize:Number;	// 滑动跑道长度

	protected var _edge:Number;			// 边长度
	
	protected var _thumbSize:Number;	// 滑动按钮长度

	protected var _oldStatus:Number = 0;	// 状态值缓存(???暂时无用)
	
	
	
	/**
	 * 滚动条大小
	 * <TODO>两侧按钮大小相同
	 * @param w				组件宽度
	 * @param h				组件高度
	 **/
	override public function setSize(w:Number, h:Number):void
	{
		if (_width == w && _height == h)
		{
			return;
		}
		if (w < 0 || h < 0) 
		{
			throw new Error("Component::setSize - 参数不可为负数！");
		}
		_width = w;
		_height = h;
		// 滚动长度
		var _trackSize:Number;
		
		// 垂直型
		if (this._isVertical)
		{
			_trackSize = h - w * 2;
			if (_trackSize < w)
			{
				throw new Error('ScrollBar::setSize(w) - 宽度参数错误！');
			}
			_edge = w;
			_backward.setSize(_edge, _edge);
			_backward.move(_edge * 0.5, _edge * 0.5);
			_slider.setSize(_edge, _trackSize);
			_slider.move(0, _edge);
			_forward.setSize(_edge, _edge);
			_forward.move(_edge * 0.5, _edge * 1.5 + _trackSize);
		}
		
		// 水平型
		else
		{
			_trackSize = w - h * 2;
			if (_trackSize < h)
			{
				throw new Error('ScrollBar::setSize(h) - 高度参数错误！');
			}
			_edge = h;
			_backward.setSize(_edge, _edge);
			_backward.move(_edge * 0.5, _edge * 0.5);
			_slider.setSize(_trackSize, _edge);
			_slider.move(_edge, 0);
			_forward.setSize(_edge, _edge);
			_forward.move(_edge * 1.5 + _trackSize, _edge * 0.5);
		}
		
		// 默认为边长度(通常情况基本是)
		this._thumbSize = _edge;
	}
	
	
	/**
	 * 设置滚动目标
	 * <TODO>调用此方法前，必须已设定好大小。
	 * @param target		滚动显示对象
	 * @param maskWidth		显示对象视域宽度
	 * @param maskHeight	显示对象视域高度
	 * @param isFront		是否在显示对象前面(否则在后面)
	 */
	public function setScrollTarget(target:DisplayObject, maskWidth:Number, maskHeight:Number, isFront:Boolean = false):void
	{
		if (!target)
		{
			throw new Error('ScrollBar::setScrollTarget(target) - 滚动显示对象不可为空对象！');
		}
		if (maskWidth <= 0 || maskHeight <= 0) 
		{
			throw new Error('ScrollBar::setScrollTarget(maskWidth||maskHeight) - 显示对象视域大小不可为负！');
		}
		this._target = target;
		this._maskWidth = maskWidth;
		this._maskHeight = maskHeight;
		
		// 定位
		if (_autoMove)
		{
			_autoMoveByTarget(isFront);
		}
		
		// 刷新
		_updateScroll();
	}

	
	/**
	 * 毁灭
	 */
	override public function destroy():void
	{
		super.destroy();
		_backward.destroy();
		_forward.destroy();
		_slider.destroy();
		if (_target)
		{
			_target = null;
		}
	}
	
	
	
	
	
	
	/* ----------------------------------------------------    get / set    ----------------------------------------------------*/
	
	
	
	
	
	
	// 滚动条皮肤
	override public function set skin(value:ISkin):void
	{
		if (!value is ScrollSkin)
		{
			throw new Error('ScrollBar::skin - 皮肤类型错误！');
		}
		this._skin = value;
		if (value)
		{
			this._backward.skin = ScrollSkin(value).backward;
			this._forward.skin = ScrollSkin(value).forward;
			this._slider.skin = ScrollSkin(value).slider;
		}
	}

	
	// 滑动按钮长度
	public function set thumbSize(value:Number):void
	{
		_thumbSize = value;
		_rejustThumbSize();
	}
	
	public function get thumbSize():Number
	{
		return _thumbSize;
	}
	
	
	// 自动隐藏滚动条
	public function set autoHide(b:Boolean):void
	{
		_autoHide = b;
		update();
	}
	public function get autoHide():Boolean
	{
		return _autoHide;
	}

	// 显示对象
	public function get target():DisplayObject
	{
		return this._target;
	}
	
	// 滚动值(状态值)
	public function set status(value:Number):void
	{
		this._slider.status = this._isVertical?this.max - value:value;
	}
	
	public function get status():Number
	{
		return this._slider.status;
	}
	
	// 最大值
	public function set max(value:Number):void
	{
		this._slider.max = value;
	}
	
	public function get max():Number
	{
		return this._slider.max;
	}
	
	
	// 两侧按钮滚动像素
	public function set roll(value:int):void
	{
		_btnRoll = value;
	}
	
	public function get roll():int
	{
		return _btnRoll;
	}
	
	// 滑动器
	public function get slider():Slider
	{
		return _slider;
	}
	
	
	
	
	
	/* ----------------------------------------------------    private    ----------------------------------------------------*/
	
	
	
	

	/**
	 * 子元素
	 */
	override protected function _addChildren():void
	{
		//this.mouseEnabled = false;
		// 滑动器
		this._slider = new Slider(0, 0, null, null, this._isVertical);
		addChild(this._slider);
		
		// 后退按钮
		this._backward = new Button(0, 0, null, null,false);
		this._backward.interval = 0.13;
		addChild(this._backward);
		this._backward.setObserverButtonDown(function():void
		{
			_slider.status -= !_isVertical ?_btnRoll: -_btnRoll;
		});
		
		// 前进按钮
		this._forward = new Button(0, 0, null, null,false);
		this._forward.interval= 0.13;
		addChild(this._forward);
		this._forward.setObserverButtonDown(function():void
		{
			_slider.status += !_isVertical? _btnRoll: -_btnRoll;
		});
	}
	
	/**
	 * 绘制
	 **/
	override protected function _draw():void
	{
		_updateScroll();
	}
	
	/**
	 * 更新滚动
	 * <TODO>当显示对象大小改变时调用！
	 * @param status		状态值(默认为负，取最小值)
	 **/
	protected function _updateScroll():void
	{
		// 垂直滚动设置显示对象
		if (this._isVertical)
		{
			this.max = this._target.height - this._maskHeight;
			this._slider.setObserverChange(function():void
			{
				_target.scrollRect = new Rectangle(_target.scrollRect.x, max - status, _maskWidth, _maskHeight);
			})
		}
		
		// 水平滚动设置显示对象
		else
		{
			this.max = this._target.width - this._maskWidth;
			this._slider.setObserverChange(function():void
			{
				_target.scrollRect = new Rectangle(status, _target.scrollRect.y, _maskWidth, _maskHeight);
			})
		}
		
		// ???
		_target.scrollRect = new Rectangle(0, status,_maskWidth, _maskHeight);

		// 调整thumb位置
		_rejustThumbSize();
	}
	
	/**
	 * 调整thumb位置
	 */
	protected function _rejustThumbSize():void
	{
		if (this._isVertical)
		{
			this._slider.setThumbSize(this._thumbSize, (this._height - _edge * 2) *this._maskHeight/(this.max+ this._maskHeight));
		}
		else
		{
			this._slider.setThumbSize((this._width - _edge * 2) *this._maskWidth/( this._maskWidth+this.max), this._thumbSize);
		}
	}

	/**
	 * 通过目标锁定位置
	 * @param isFront		在目标前方
	 **/
	protected function _autoMoveByTarget(isFront:Boolean):void
	{
		if (_target)
		{	
			// 垂直
			if (_isVertical)
			{
				if (isFront)
				{
					move(this._target.x - this._width - 0.5 , this._target.y);
				}
				else
				{
					move(this._target.x + this._maskWidth + 0.5 , this._target.y);
				}
			}
			
			// 水平
			else
			{
				if (isFront)
				{
					move(this._target.x, this._target.y - this._height - 0.5 );
				}
				else
				{
					move(this._target.x, this._target.y +this._maskHeight + 0.5 );
				}
			}
		}
	}
}

}